//
//  PlayMusic.swift
//  YYMusicDemo
//
//  Created by yuanyue on 2018/4/27.
//  Copyright © 2018年 yuanyue. All rights reserved.
//

import UIKit
import AVFoundation

class PlayMusic: NSObject {
    
    var player : AVAudioPlayer?
    
    func playMusic(musicName : String) -> () {
        guard let path = Bundle.main.url(forResource: musicName, withExtension: nil) else {return}
        if player?.url == path {
            player?.play()
            return
        }
        
        do{
            player = try AVAudioPlayer(contentsOf: path)

        }catch {
            print(error)
            return
        }
        
        player?.prepareToPlay()
        player?.play()

    }
    
    func pasueMusic() -> () {
        player?.pause()
    }
    

}
